package com.example.administrator.reader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.administrator.reader.R;
import com.example.administrator.reader.core.AppStatusTracker;
import com.example.administrator.reader.core.BaseActivity;
import com.example.administrator.reader.fragment.BaseFragment;
import com.example.administrator.reader.utils.ConstantValues;
import com.example.administrator.reader.utils.FragmentUtils;
import com.example.administrator.reader.utils.UIUtils;
import com.example.administrator.reader.view.BookView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

public class MainActivity extends BaseActivity {
    @ViewInject(R.id.rg)
    RadioGroup rg;
    @ViewInject(R.id.vp)
    ViewPager vp;


    private LinearLayout layout_delete;
    private Button bt_choose_all;
    private Button bt_choose_one;

    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_main, R.string.shelf, -1, MODE_HOME);
        ViewUtils.inject(this);
    }

    @Override
    protected void setUpView() {
        layout_delete = (LinearLayout) findViewById(R.id.layout_delete);
        bt_choose_all = (Button) findViewById(R.id.bt_choose_all);
        bt_choose_one = (Button) findViewById(R.id.bt_choose_one);
    }

    @Override
    protected void setUpData(Bundle savedInstanceState) {
        // 开始进入主界面，RadioGroup 先选中 第一个；
        rg.check(R.id.rb_1);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_1:
                        vp.setCurrentItem(0);
                        setUpTitle(R.string.shelf);
                        setUpMenu(-1);
                        break;
                    case R.id.rb_2:
                        vp.setCurrentItem(1);
                        setUpTitle(R.string.city);
                        setUpMenu(R.menu.menu_home);
                        break;
                    case R.id.rb_3:
                        vp.setCurrentItem(2);
                        setUpTitle(R.string.user);
                        setUpMenu(-1);
                        break;
                }
            }
        });
        // 需要获得支持Fragment的管理器，所以需要继承至FragmentActiviy；
        vp.setAdapter(new MyAdapter(getSupportFragmentManager()));

        vp.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            // 当ViewPager页面改变时，进行刷新数据
            @Override
            public void onPageSelected(int position) {
                BaseFragment fragment = FragmentUtils.getFragment(position);
                fragment.loadData();
            }
        });

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_search:
                startActivity(new Intent(MainActivity.this, CitySearch.class));
                break;
        }
        return true;
    }


    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return FragmentUtils.getFragment(position);
        }

        @Override
        public int getCount() {
            return 3;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != BookView.instance) {
            BookView.instance.startCloseBookAnimation();
        }
    }

    public Button getBt_choose_all() {
        return bt_choose_all;
    }

    public Button getBt_choose_one() {
        return bt_choose_one;
    }

    public RadioGroup getRg() {
        return rg;
    }

    public LinearLayout getLayout_delete() {
        return layout_delete;
    }
}
